package cn.liutao.dish.service;


import cn.liutao.common.enums.CommonEnum;
import cn.liutao.common.utils.ServletUtils;
import cn.liutao.dish.entity.param.DishParam;
import cn.liutao.dish.entity.po.Dish;
import cn.liutao.dish.entity.po.Taste;
import cn.liutao.dish.entity.vo.DishVo;
import cn.liutao.dish.mapper.DishMapper;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryOrderBy;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.relation.RelationManager;
import com.mybatisflex.core.row.Db;
import com.mybatisflex.core.row.DbChain;
import lombok.extern.slf4j.Slf4j;

import static cn.liutao.dish.entity.po.table.DishTableDef.DISH;
/**
 * 菜品表 服务层实现。
 *
 * @author liutao
 * @since 2023-11-01
 */

@Slf4j
public class DishService {
    private static volatile DishService instance;
    private DishMapper dishMapper;
    //私有构造方法
    private DishService(){
        dishMapper = ServletUtils.getMapper(DishMapper.class);
    }

    /**
     * 双重检查锁实现单例
     * @return
     */
    public static DishService getInstance(){
        if (instance==null){
            synchronized (DishService.class){
                if (instance==null){
                    instance = new DishService();
                }
            }
        }
        return instance;
    }


    public Page<DishVo> selectByList(Dish queryParam) {
        QueryWrapper qw = QueryWrapper.create()
                .where(DISH.STATUS.eq(queryParam.getStatus()))
                .and(DISH.NAME.like(queryParam.getName()))
                .and(DISH.PRICE.eq(queryParam.getPrice()))
                .and(DISH.CUISINE_ID.eq(queryParam.getCuisineId()))
                .and(DISH.TYPE.eq(queryParam.getType()))
                .orderBy(new QueryOrderBy(DISH.SORT),new QueryOrderBy(DISH.ID))
                ;
        RelationManager.addExtraConditionParam("status",CommonEnum.STATUS_ON.getIntValue());
        Page<DishVo> paginate = dishMapper.paginateWithRelationsAs(queryParam.getPageNum(), queryParam.getPageSize(), qw,DishVo.class);
        return paginate;
    }

    public void insert(DishParam addParam) {
        //开启事务管理
        Db.tx(()->{
            dishMapper.insertSelective(addParam);
            addItem(addParam);
            return true;
        });


    }

    public void updateById(DishParam editParam) {
        Db.tx(()->{
            dishMapper.update(editParam);
            deleteItem(editParam.getId());
            addItem(editParam);
            return true;
        });
    }

    public void deleteById(Integer id) {
        Db.tx(()->{
            deleteItem(id);
            dishMapper.deleteById(id);
            return true;
        });
    }

    /**
     * 获取详情
     * @param id
     * @return
     */
    public DishVo selectById(Integer id) {
        QueryWrapper qw = QueryWrapper.create().where(DISH.ID.eq(id));
        RelationManager.addExtraConditionParam("status",CommonEnum.STATUS_ON.getIntValue());
        DishVo dishVo = dishMapper.selectOneWithRelationsByQueryAs(qw, DishVo.class);
        return dishVo;
    }

    /**
     * 删除明细
     * @param id
     */
    public void deleteItem(Integer id){
        //删除
        DbChain.table("dish_taste_relation")
                .where("dish_id=?",id)
                .remove();
    }

    /**
     * 新增明细
     * @param dishParam
     */
    public void addItem(DishParam dishParam){
        //新增
        for (Taste taste : dishParam.getItemList()) {
            DbChain.table("dish_taste_relation")
                    .set("dish_id",dishParam.getId())
                    .set("taste_id",taste.getId())
                    .save();
        }
    }
}
